const mongoose = require('mongoose');

const settingsSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true,
    unique: true
  },
  // 界面设置
  interface: {
    theme: {
      mode: {
        type: String,
        enum: ['light', 'dark', 'system'],
        default: 'light'
      },
      primaryColor: {
        type: String,
        default: '#1a5cff'
      },
      compact: {
        type: Boolean,
        default: false
      },
      animations: {
        type: Boolean,
        default: true
      }
    },
    language: {
      type: String,
      default: 'zh-CN'
    },
    timezone: {
      type: String,
      default: 'Asia/Shanghai'
    }
  },
  // 通知设置
  notifications: {
    deployStart: {
      type: Boolean,
      default: true
    },
    deployComplete: {
      type: Boolean,
      default: true
    },
    deployFailed: {
      type: Boolean,
      default: true
    },
    channels: {
      browser: {
        type: Boolean,
        default: false
      },
      email: {
        type: Boolean,
        default: false
      }
    }
  },
  // 部署设置
  deployment: {
    defaultBranch: {
      type: String,
      default: 'master'
    },
    autoCleanup: {
      type: Boolean,
      default: true
    },
    maxDeployLogs: {
      type: Number,
      default: 50
    }
  },
  // 登录设备
  loginDevices: [{
    deviceId: String,
    deviceName: String,
    deviceType: String,
    ip: String,
    lastLogin: Date,
    userAgent: String,
    isActive: {
      type: Boolean,
      default: true
    }
  }]
}, {
  timestamps: true
});

// 添加索引
settingsSchema.index({ userId: 1 });
settingsSchema.index({ 'loginDevices.deviceId': 1 });

// 添加方法
settingsSchema.methods.addLoginDevice = async function(deviceInfo) {
  const existingDevice = this.loginDevices.find(d => d.deviceId === deviceInfo.deviceId);
  if (existingDevice) {
    existingDevice.lastLogin = new Date();
    existingDevice.ip = deviceInfo.ip;
    existingDevice.userAgent = deviceInfo.userAgent;
  } else {
    this.loginDevices.push({
      ...deviceInfo,
      lastLogin: new Date(),
      isActive: true
    });
  }
  return this.save();
};

settingsSchema.methods.removeLoginDevice = async function(deviceId) {
  const device = this.loginDevices.find(d => d.deviceId === deviceId);
  if (device) {
    device.isActive = false;
  }
  return this.save();
};

// 静态方法
settingsSchema.statics.getOrCreate = async function(userId) {
  let settings = await this.findOne({ userId });
  if (!settings) {
    settings = await this.create({ 
      userId,
      interface: {
        theme: {
          mode: 'light',
          primaryColor: '#1a5cff',
          compact: false,
          animations: true
        },
        timezone: 'Asia/Shanghai'
      },
      notifications: {
        deployStart: true,
        deployComplete: true,
        deployFailed: true,
        channels: {
          browser: false,
          email: false
        }
      }
    });
  }
  return settings;
};

const Settings = mongoose.model('Settings', settingsSchema);

module.exports = Settings; 